Page({
  data: {
    activeTab: 'system_prompt', // 默认选中系统提示
    messageList: [],
    recentChats: [] // 新增
  },

  onLoad: function (options) {
    this.loadMessages(this.data.activeTab);
  },

  onTabChange: function (e) {
    const newTab = e.currentTarget.dataset.tab;
    this.setData({ activeTab: newTab });
    if (newTab === 'chat_with_engineer') {
      this.loadRecentChats();
    } else {
      this.loadMessages(newTab);
    }
  },

  loadMessages: function (tab) {
    let messages = [];
    if (tab === 'system_prompt') {
      messages = [
        {
          id: 1,
          type: 'system-prompt',
          icon: '/assets/images/yuan.png',
          title: '您有新的提现金额！',
          description: '系统提示系统提示内容文案内容文案系统......',
          unread: true
        },
        {
          id: 2,
          type: 'device-operation',
          icon: '/assets/images/fang.png',
          title: '设备运营通知！',
          description: '系统提示系统提示内容文案内容文案系统......',
          unread: false
        },
        {
          id: 3,
          type: 'warning',
          icon: '/assets/images/san.png',
          title: '设备到期停止运营通知！',
          description: '系统提示系统提示内容文案内容文案系统......',
          unread: true
        }
      ];
      this.setData({ messageList: messages });
    } else if (tab === 'system_notification') {
      // 从后端接口拉取公告
      const app = getApp();
      wx.request({
        url: `${app.globalData.baseUrl}/api/announcements`, // Spring Boot后端公告接口
        method: 'GET',
        success: res => {
          const notices = (res.data || []).map(item => ({
            id: item.id,
            type: 'system-notification',
            icon: '/assets/images/tixian.png', // 可根据类型自定义
            title: item.title,
            description: item.content,
            unread: false
          }));
          this.setData({ messageList: notices });
        },
        fail: () => {
          this.setData({ messageList: [] });
        }
      });
    } else if (tab === 'warning_notification') {
      messages = [
        {
          id: 5,
          type: 'warning-notification',
          icon: '/assets/images/tixian.png',
          title: '这是一条警告通知',
          description: '警告通知内容警告通知内容警告通知内容......',
          unread: true
        }
      ];
      this.setData({ messageList: messages });
    }
  },

  clearUnread: function () {
    // 清除所有未读消息的逻辑
    const updatedList = this.data.messageList.map(item => ({
      ...item,
      unread: false
    }));
    this.setData({
      messageList: updatedList
    });
    wx.showToast({
      title: '已清除所有未读',
      icon: 'none'
    });
  },

  onReady: function () {

  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {

  },

  onReachBottom: function () {

  },

  onShareAppMessage: function () {

  },

  onNoticeTap: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/noticeDetail/noticeDetail?id=' + id
    });
  },

  // 新增：加载最近聊天工程师
  loadRecentChats: function () {
    const userId = wx.getStorageSync('userId');
    const app = getApp();
    wx.request({
      url: `${app.globalData.baseUrl}/api/recent-chats?userId=${userId}`,
      method: 'GET',
      success: res => {
        this.setData({
          recentChats: res.data
        });
      },
      fail: () => {
        this.setData({ recentChats: [] });
      }
    });
  },

  // 新增：点击工程师卡片进入聊天
  onEngineerChatTap: function(e) {
    const { engineerid, orderid } = e.currentTarget.dataset;
    const userId = wx.getStorageSync('userId');
    wx.navigateTo({
      url: `/pages/chat/chat?orderId=${orderid}&userId=${userId}&engineerId=${engineerid}`
    });
  }
}) 